#include <stdio.h>
#include <stdlib.h>
#include <gmssl/sm2.h>

void print_hex(const char *label, const uint8_t *data, size_t len) {
    printf("%s: ", label);
    for(size_t i = 0; i < len; i++) {
        printf("%02X", data[i]);
    }
    printf("\n");
}

int main() {
    SM2_KEY key;
    
    // 生成SM2密钥对
    if (sm2_key_generate(&key) != 1) {
        fprintf(stderr, "SM2密钥生成失败\n");
        return EXIT_FAILURE;
    }
    
    // 保存私钥到文件
    FILE *priv_file = fopen("alice_private_key.bin", "wb");
    if (!priv_file) {
        perror("无法打开私钥文件");
        return EXIT_FAILURE;
    }
    fwrite(&key, sizeof(SM2_KEY), 1, priv_file);
    fclose(priv_file);
    
    // 保存公钥到文件
    FILE *pub_file = fopen("alice_public_key.bin", "wb");
    if (!pub_file) {
        perror("无法打开公钥文件");
        return EXIT_FAILURE;
    }
    fwrite(&key, sizeof(SM2_KEY), 1, pub_file);
    fclose(pub_file);
    
    printf("Alice密钥对生成成功\n");
    printf("私钥已保存至: alice_private_key.bin\n");
    printf("公钥已保存至: alice_public_key.bin\n");
    
    return EXIT_SUCCESS;
}
